/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Model;

import Helper.RoomHelper;
import common.Constant;
import java.util.ArrayList;
import javax.swing.table.AbstractTableModel;


/**
 *
 * @author hxbien
 */
public class RoomModel extends AbstractTableModel{

    ClassLoader c = this.getClass().getClassLoader();

    ArrayList<RoomHelper> RoomList;
    private final String colunmsName[] = { Constant.getBundle().getString("id"), Constant.getBundle().getString("roomName") };
    private final Class columnsClass[] = { String.class, String.class };

    public RoomModel(ArrayList<RoomHelper> RoomList){
        this.RoomList = RoomList;
    }
    @Override
    public int getRowCount() {
        return RoomList.toArray().length;
    }

    @Override
    public int getColumnCount() {
        return colunmsName.length;
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
         switch(columnIndex){
            case 0 :
                return RoomList.get(rowIndex).getRoomID();
            case 1 :
                return RoomList.get(rowIndex).getRoomName();
            case 2 :
                return RoomList.get(rowIndex).isCheck();
            default:
                return null;
        }
    }
     @Override
    public void setValueAt(Object val, int rowIndex, int columnIndex) {
//nếu edit tại column Boolean
        if (columnIndex == 2)
//thì lấy giá trị mới sau khi edit (val) gán vào data của model
            RoomList.get(rowIndex).setCheck(Boolean.parseBoolean(val.toString()));
        fireTableRowsUpdated(rowIndex, rowIndex);
    }
      @Override
    public String getColumnName(int icol){
        return colunmsName[icol];
    }

    @Override
    public Class getColumnClass(int icol){
        return columnsClass[icol];
    }

    @Override
    public boolean isCellEditable(int rowIndex, int columnIndex) {
        return columnIndex == 2;
    }


     public void deleteRow(int deleteRow) {
        RoomList.remove(deleteRow);
        RoomList.trimToSize();
        fireTableRowsDeleted(deleteRow, deleteRow);
    }
}
